/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.bookmarks.server.sharing;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.gadglet.core.GadgletRequestWrapper;
import com.gadglet.core.GadgletResponse;
import com.gadglet.core.MingletRequestHandler;
import com.gadglet.core.RequestException;
import com.gadglet.data.SharedItemReff;
import com.gadglet.data.utils.ContentItemHelper;
import com.gadglet.data.utils.PMF;
import com.gadglet.gadgets.bookmarks.server.BookMarksList;
import com.gadglet.params.ReqErrorTypes;
import com.gadglet.params.SharingType;
/**
 * Gadglet server side handler class. This class is created to demonstrate 
 * Gadglet sub RequestHandler. By using the request method setCustomContentName("sub package name")
 * it is possible to call a sub RequestHandler in a package located under the Gadget server 
 * root.
 * 
 * @see com.gadglet.params.ReqActionTypes
 * @see MingletRequestHandler
 */
public class RequestHandler extends MingletRequestHandler {
	Logger log = Logger.getLogger(this.getClass().getName());

	private final ContentItemHelper helper = new ContentItemHelper();

	/**
	 * "Hide" a personal note that was shared with you
	 * */
	@Override
	public void doDelete(GadgletRequestWrapper request, GadgletResponse response)
			throws RequestException {
		PersistenceManager pm;		
		BookMarksList item;
		List<SharedItemReff> sharing ;
		
		pm = PMF.get().getPersistenceManager();
		pm.currentTransaction().begin();
		try {
			item = (BookMarksList) helper.getSingleItemByKeyForAction(pm, request, BookMarksList.class);

			if (item == null)
				throw new RequestException(ReqErrorTypes.ITEM_NOT_FOUND);
			
			sharing = 	item.getSharedItemReff();
			
			for(SharedItemReff share: sharing)
			{
				// in case not the owner (shared item)
				if(share.getSharedWithId().equals(request.getCurrentUserId()) && !share.getSharingType().equals(SharingType.OWNER))
					sharing.remove(share);
			
			}

			pm.makePersistent(item);
			pm.currentTransaction().commit();
		} catch (Exception e) {
			log.warning(e.getMessage());
			throw new RequestException(ReqErrorTypes.REQUEST_FAILED);
		} finally {
			if (pm.currentTransaction().isActive())
				pm.currentTransaction().rollback();
			pm.close();
		}
	}

	
	

	@Override
	public void doAdd(GadgletRequestWrapper request, GadgletResponse response)throws RequestException{
		throw new RequestException(ReqErrorTypes.UNRECOGNIZED_ACTION);
	}

	@Override
	public void doUpdate(GadgletRequestWrapper request, GadgletResponse response)
			throws RequestException {
		throw new RequestException(ReqErrorTypes.UNRECOGNIZED_ACTION);
	}


	@Override
	public void doView(GadgletRequestWrapper request, GadgletResponse response)
			throws RequestException {
		throw new RequestException(ReqErrorTypes.UNRECOGNIZED_ACTION);
	}

	
	
}
